
package com.hd.sup.modules.district.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.core.common.dto.ZTreeDTO;
import com.hd.common.utils.UserUtils;
import com.hd.sup.modules.district.model.Region;
import com.hd.sup.modules.district.service.IRegionService;
import com.hd.sup.modules.enterprise.service.IEnterpriseService;
import com.hd.sup.modules.vehicle.service.IVehicleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title RegionController.java
 * @Description 省市县区域控制器
 * @Package com.hd.sup.modules.district.controller
 * @date 2020/12/29 12:32 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/region")
public class RegionController extends BaseController {

    @Resource
    private UserUtils userUtils;

    @Resource
    private IRegionService regionService;

    @Resource
    private IEnterpriseService enterpriseService;

    @Resource
    private IVehicleService vehicleService;

    /**
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author chentl
     * @description 获取树形数据
     * @date 2020/9/10 11:25 上午
     * @params [teamsParam]
     */
    @PostMapping("/treeData")
    public Result<?> treeData(@RequestBody RequestData requestData) {

        Integer regionId = requestData.getInteger("regionId");
        if (regionId == null) return Result.error("参数 regionId 为空");

        //封装企业结构树
        List<ZTreeDTO> list = regionService.getTreeData(regionId);
        list = ZTreeDTO.buildTree(list);

        return Result.success(list);
    }


    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 省市区列表查询
     * @date 2020/12/31 2:57 下午
     * @params [requestData]
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody RequestData requestData) {
        String regionName = requestData.getString("regionName");
        Integer pid = requestData.getInteger("pid");
        Region param = new Region(regionName, pid);


        List<Map<String, Object>> result = regionService.getRegionList(param);
        return Result.success(result);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 删除省市区县信息
     * @date 2020/12/31 3:25 下午
     * @params [ids]
     */
    @DeleteMapping("/delete/{ids}")
    public Result<?> delete(@PathVariable() String ids) {
        if (StringUtils.isBlank(ids)) return Result.error("参数 ids 为空");
        regionService.removeByIds(Arrays.stream(ids.split(BizConstant.Common.SEPARATOR.key())).map(ValueUtils::parseInteger).collect(Collectors.toList()));
        return Result.success();
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 保存或更新
     * @date 2020/12/31 3:37 下午
     * @params [ids]
     */
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(Region region) {
        //入参校验
        if (region == null) return Result.error("参数为空");

        List<Region> db = regionService.list(new LambdaQueryWrapper<Region>().orderByDesc(Region::getId));
        Integer nextId = CollectionUtils.isNotEmpty(db) ? db.get(0).getId() + 1 : 1;

        if (region.getId() == null) region.setId(nextId);

        region.insertOrUpdate();
        return Result.success(region);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查指定行政区域下的 企业数 和 车辆总数
     * @date 2021/1/5 6:13 下午
     * @params [region]
     */
    @PostMapping("/getRegionEVNum")
    public Result<?> getRegionEVNum(@RequestBody RequestData requestData) {
        String regionCode = requestData.getString("regionCode");
        if (StringUtils.isBlank(regionCode)) return Result.error("参数 regionCode 为空");

        List<String> deptIds = userUtils.getDeptIdList();
        Map<String, Object> resultMap = Maps.newHashMap();
        Integer enterprise_sum = enterpriseService.countEnterpriseNum(regionCode, deptIds);
        Integer vehicle_sum = vehicleService.countVehicleNum(regionCode, null, deptIds);

        resultMap.put("enterprise_sum", enterprise_sum);
        resultMap.put("vehicle_sum", vehicle_sum);
        return Result.success(resultMap);
    }


}
